#include "TableInterface/table.h"
#include "fpga_addr_define.h"
#include "dataBase/miscStaticDataFunction.h"
#include "function_interface.h"
#include "fpga_interface.h"
#include "utils.h"
#include "debug.h"


/* 从数据库回读参数到参数表 */
int ParamTable_InitCallback(tableEntry_t *pEntry)
{
    int retVal = 0;
    uint32_t addr = pEntry->idx;
    uint16_t *pValue = &pEntry->data;
    struct commonData commData;

    MSD_MicroFileRead(MICRO_FILE_ID_PARAM, addr, &commData);
    // PRINT_DEBUG("Read from database: [%d]=%d",  addr, commData.cd_dataValue.uint16Value);
    *pValue = commData.cd_dataValue.uint16Value;

    if(addr < FPGA_ADDR_MAX / 2)
    {
        switch(addr)
        {
            /* 不操作的地址 */
            case FPGA_ADDR_CMD_BEGIN + 0:
            case FPGA_ADDR_ARM_FAULT_REG_BEGIN + 0:
            case FPGA_ADDR_ARM_FAULT_REG_BEGIN + 1:
            case FPGA_ADDR_ARM_FAULT_REG_BEGIN + 2:
            case FPGA_ADDR_220:
            case FPGA_ADDR_221:
            case FPGA_ADDR_222:
                break;

            default:
                FpgaInf_Write(addr, *pValue); 
                break;
        }
    }

    return retVal;
}


int ParamTable_RefreshCallback(tableEntry_t *pEntry)
{
    return 0;
}


/* 写参数到参数表和数据库 */
int ParamTable_SetCallback(tableEntry_t *pEntry)
{
    int retVal = 0;
    uint32_t addr = pEntry->idx;
    uint16_t value = pEntry->data;
    struct commonData commData;

    switch(addr)
    {
        /* 不保存的地址 */
        case FPGA_ADDR_CMD_BEGIN + 0:
        case FPGA_ADDR_ARM_FAULT_REG_BEGIN + 0:
        case FPGA_ADDR_ARM_FAULT_REG_BEGIN + 1:
        case FPGA_ADDR_ARM_FAULT_REG_BEGIN + 2:
        case FPGA_ADDR_220:
        case FPGA_ADDR_221:
        case FPGA_ADDR_222:
            return retVal;

        default:
            commData.cd_dataType = COM_DATA_TYPE_UINT16;
            commData.cd_dataValue.uint16Value = value;
            MSD_MicroFileWrite(MICRO_FILE_ID_PARAM, addr, &commData);
            return retVal;
    }    
}

